package android

import (
	"context"
	"fmt"
	"log"
	"os"
)

var Log = NewLogger()

type Logger struct {
	ctx      context.Context
	logger   *log.Logger
	preHooks []func(ctx context.Context, log string)
}

func NewLogger() *Logger {
	return &Logger{logger: log.New(os.Stdout, "", log.Ldate|log.Ltime|log.Lshortfile)}
}

func (l *Logger) SetContext(ctx context.Context) {
	l.ctx = ctx
}

func (l *Logger) AddPreHook(hook func(ctx context.Context, log string)) {
	l.preHooks = append(l.preHooks, hook)
}

func (l *Logger) Println(v ...any) {
	l.doHook(fmt.Sprint(v))
	l.logger.Println(v)

}

func (l *Logger) Printf(format string, v ...any) {
	l.doHook(fmt.Sprintf(format, v))
	l.logger.Printf(format, v)
}

func (l *Logger) doHook(log string) {
	for _, hook := range l.preHooks {
		hook(l.ctx, log)
	}
}
